using System;
using System.Web;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Util;
using newtelligence.DasBlog.Web.Core;
using newtelligence.DasBlog.Web.Services.Rss20;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace newtelligence.DasBlog.Web.Services
{
    public partial class WebWebServices
	{
		
        [OperationContract, XmlSerializerFormat, WebGet(UriTemplate="/sitemap")]
        public urlset GoogleSitemapHandler()
        {
            try
            {
				//Cache the sitemap for 12 hours...
				string CacheKey = "GoogleSiteMap";
				urlset root = System.Web.Hosting.HostingEnvironment.Cache[CacheKey] as urlset;
				if (root == null) //we'll have to build it...
				{
					root = new urlset();
					root.url = new urlCollection();

					//Default first...
					url basePage = new url(Utils.GetBaseUrl(siteConfig),DateTime.Now,changefreq.daily,1.0M);
					root.url.Add(basePage);

					url defaultPage = new url(Utils.GetStartPageUrl(siteConfig),DateTime.Now,changefreq.daily,1.0M);
					root.url.Add(defaultPage);

					//Archives next...
					url archivePage = new url(Utils.RelativeToRoot(siteConfig, "archives.aspx"),DateTime.Now,changefreq.daily,1.0M);
					root.url.Add(archivePage);

					//All Pages
                    EntryCollection entryCache = dataService.GetAllEntries(true);
                    foreach (Entry e in entryCache)
                    {
                        if (e.IsPublic)
                        {
                            //Start with a RARE change freq...newer posts are more likely to change more often.
                            // The older a post, the less likely it is to change...
                            changefreq freq = changefreq.daily;

                            //new stuff?
                            if (e.CreatedLocalTime < DateTime.Now.AddMonths(-9))
                            {
                                freq = changefreq.yearly;
                            }
                            else if (e.CreatedLocalTime < DateTime.Now.AddDays(-30))
                            {
                                freq = changefreq.monthly;
                            }
                            else if (e.CreatedLocalTime < DateTime.Now.AddDays(-7))
                            {
                                freq = changefreq.weekly;
                            }
                            if (e.CreatedLocalTime > DateTime.Now.AddDays(-2))
                            {
                                freq = changefreq.hourly;
                            }

                            //Add comments pages, since comments have indexable content...
                            // Only add comments if we aren't showing comments on permalink pages already
                            if (siteConfig.ShowCommentsWhenViewingEntry == false)
                            {
                                url commentPage = new url(Utils.GetCommentViewUrl(siteConfig, e.EntryId), e.CreatedLocalTime, freq, 0.7M);
                                root.url.Add(commentPage);
                            }

                            //then add permalinks
                            url permaPage = new url(Utils.GetPermaLinkUrl(siteConfig, e), e.CreatedLocalTime, freq, 0.9M);
                            root.url.Add(permaPage);
                        }
                    }

					//All Categories
					CategoryCacheEntryCollection catCache = dataService.GetCategories();
					foreach (CategoryCacheEntry cce in catCache)
					{
						if (cce.IsPublic)
						{
							url catPage = new url(Utils.GetCategoryViewUrl(siteConfig,cce.Name),DateTime.Now,changefreq.weekly,0.6M);
							root.url.Add(catPage);
						}
					}
					System.Web.Hosting.HostingEnvironment.Cache.Insert(CacheKey,root,null,DateTime.Now.AddHours(12),System.Web.Caching.Cache.NoSlidingExpiration);
				}

                return root;
			}
            catch(Exception exc)
            {
                ErrorTrace.Trace(System.Diagnostics.TraceLevel.Error,exc);
            }
            return null;
        }
	}
}
